// pages/search/search.js



import request from '../../utils/request';
var isSend =  false;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    plaContent:'',
    hotList:[],
    inputContent:'',
    searchList:[]
  },

  toPlay(event){
    let songId = event.currentTarget.dataset.song
    wx.navigateTo({
      url: '../player/player?ids=' + songId.id,
    })
  },

  inputChange(event){
    this.setData({
      inputContent: event.detail.value.trim()
    })
    // 函数节流!!!!!!!!!!!!!
    if(isSend){
      return
    }
    isSend =true
    this.getSongsData()
    setTimeout(()=>{
      isSend=false
    },300)

  },

  // 获取搜索数据
  async getSongsData(){
    if(!this.data.inputContent){
      return
    }
    let searchData= await request('/search',{keywords:this.data.inputContent,limit:20})
    this.setData({
      searchList: searchData.result.songs
    })
  },

  //获取初始化数据
  async getInitData(){
    let placeholderDate = await request('/search/default');
    let hotListDate = await request('/search/hot/detail');
    this.setData({
      plaContent: placeholderDate.data.showKeyword,
      hotList: hotListDate.data
    })
  },

  cancleSearch(){
    this.setData({
      inputContent:''
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getInitData();
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})